﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Rushabh.Logic.Missiles
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Missile
    {
        Vector2 velocity;        
        public Rectangle _destRect;
        public Vector2 _position;
        public double _angle;
        public Texture2D missileTexture;
        public Vector2 acceleration = new Vector2(5.1f, 0.1f);
        public Vector2 _destination;
        private Vector2 _incrementationVector;
        private bool posflag = false;
        private bool posflag2 = false;
        public bool active = true;

        public Missile(ContentManager content, Vector2 source, Vector2 destination)
        {
            missileTexture = content.Load<Texture2D>("wp/stick");
            _position = source;
            _destination = destination;

        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            _destRect = new Rectangle((int)_position.X, (int)_position.Y,10, 10);
            _incrementationVector = new Vector2();
            _incrementationVector.X = (_position.X - _destination.X);
            _incrementationVector.Y = (_position.Y - _destination.Y);
            if (_incrementationVector.X > 0)
                posflag = true;
            if (_incrementationVector.Y < 0)
                posflag2 = true;
            if (_incrementationVector.Y == 0)
            {

            }
            _angle = Math.Atan(_incrementationVector.Y / _incrementationVector.X);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTimerEventArgs gameTime)
        {
            _incrementationVector.X = 1f * (posflag ? -1 : 1);
            _incrementationVector.Y = (float)Math.Tan(_angle) * _incrementationVector.X;
            _incrementationVector.Normalize();
            _position += _incrementationVector * acceleration;
            _destRect.X = (int)_position.X;
            _destRect.Y = (int)_position.Y;
        }

        internal void Draw(SpriteBatch spriteBatch)
        {
            if (active)
            {
                if (_destRect.Width == 0)
                    _destRect.Width = 10;
                if (_destRect.Height == 0)
                    _destRect.Height = 10;
                spriteBatch.Draw(missileTexture, _destRect, Color.White);
            }
            //float rot = (float)((_destination.Y - _position.Y) / (_destination.X - _position.X));
            //var temp = MathHelper
            //    .ToDegrees(rot);
            //spriteBatch.Draw(missileTexture, _destRect, null, Color.White, rot, _position, SpriteEffects.None, 1);
        }

       
    }
}


//using System;
//using System.Net;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Ink;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;
//using TappUp_Refresh;
//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Content;

//namespace ImmunityBuster_wp7_2.Logic
//{
//    public class Bullet
//    {
//        public Texture2D _bullet;
//        private double _angle;
//        public Vector2 _position;
//        public Vector2 velocity;
//        private bool Active;
//        private Vector2 StartPosition;
//        private Vector2 EndPosition;
//        private Vector2 RelativeDistance;
       

//        public Bullet(String AssetName, ContentManager content, ref Vector2 StartPosition, ref Vector2 EndPosition)
//        {
//            _bullet = content.Load<Texture2D>(AssetName);
//            this.StartPosition = StartPosition;
//            this.EndPosition = EndPosition;
//            this.RelativeDistance = Vector2.Subtract(StartPosition, EndPosition);
//            this._angle = Math.Tan(RelativeDistance.X / RelativeDistance.Y);
//            this.RelativeDistance.Normalize();
//        }


//        public void Update(GameTimerEventArgs gameTime)
//        {
//            if (Active)
//            {
//                _position += RelativeDistance;            
//            }           
//        }

//        public void Draw(SpriteBatch spriteBatch)
//        {
//            spriteBatch.Draw(_bullet, _position, null, Microsoft.Xna.Framework.Color.White, (float)_angle, _position,1f, SpriteEffects.None, 1f);        
//        }


//    }
//}
